package com.example.conf;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.example.webService.server.UserService;
import com.example.webService.server.UserServiceImpl;

//@Configuration
public class WebServiceConfig {
	@Bean//配置service路径 方法名可以修改
	public ServletRegistrationBean dispatcherServlet() {
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/service/*");
		servletRegistrationBean.setName("service");
	    return servletRegistrationBean;
	}
	//??url可以进入对应的mvc方法，但是视图解析器却不起作用？？
	@Bean//配置其余路径 方法名可以修改
	public ServletRegistrationBean myServlet() {
		//注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //base package
        applicationContext.scan("com.example");
        //通过构造函数指定dispatcherServlet的上下文
        DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        //指定urlmapping
        registrationBean.addUrlMappings("/*");
        //指定name，如果不指定默认为dispatcherServlet
        return registrationBean;
	}
	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
	    return new SpringBus();
	}
	@Bean
	public UserService userService() {
	    return new UserServiceImpl();
	}
	@Bean
	public Endpoint endpoint() {
	    EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
	    endpoint.publish("/user");
	    return endpoint;
	}
	
//	@Bean
//	public SimpleJaxWsServiceExporter simpleJaxWsServiceExporter(){
//		SimpleJaxWsServiceExporter exporter=new SimpleJaxWsServiceExporter();
//		exporter.setBaseAddress("http://localhost:9090/");
//		return exporter;
//	}
}
